<template>
  <div class="app-container">
    <el-tabs v-model="tabIndex" @tab-click="changTab">
      <el-tab-pane label="商品" name="0"></el-tab-pane>
      <el-tab-pane label="配件" name="1"></el-tab-pane>
      <el-tab-pane label="物料" name="2"></el-tab-pane>
    </el-tabs>
    <el-form :model="form" ref="form" :inline="true">
      <el-form-item label="产品名称" prop="productName">
        <el-input v-model="form.productName" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="规格来源" prop="skuSource">
        <el-select v-model="form.skuSource" filterable clearable>
          <el-option
            v-for="item in dict.type.basic_good_source"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="规格名称" prop="skuName">
        <el-input v-model="form.skuName" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-button v-hasPermi="['ims:goodAttr:add']" type="primary" plain icon="el-icon-plus" @click="updateRow(0,{})">新增
    </el-button>
    <el-button type="success" plain icon="el-icon-download" @click="handleExport">导出
    </el-button>
    <el-table
      v-loading="loading"
      :data="list"
    >
      <el-table-column type="index" label="序号" align="center">
        <template slot-scope="scope">
          <span>{{ (form.pageNum - 1) * form.pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="imageUrl" label="图片" align="center" width="100">
        <template slot-scope="scope">
          <el-image
            style="width: 80px;height: 80px"
            :src="scope.row.imageUrl"
            fit="contain"
            :preview-src-list="[scope.row.imageUrl]"
          >
          </el-image>
        </template>
      </el-table-column>
      <el-table-column prop="productName" label="产品名称" align="center">
        <template slot-scope="scope">
          <el-link type="primary" @click="updateRow(1,scope.row)">{{ scope.row.productName }}</el-link>
        </template>
      </el-table-column>
      <el-table-column prop="productCategoryName" label="产品类型" align="center" width="100"/>
      <el-table-column prop="skuSource" label="规格来源" align="center" width="80">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.basic_good_source" :value="scope.row.skuSource"/>
        </template>
      </el-table-column>
      <el-table-column prop="skuName" label="规格名称" align="center"/>
      <el-table-column prop="skuCode" label="规格编码" align="center" width="100"/>
      <el-table-column prop="skuAttributeVoList" label="产品规格" align="center" >
        <template slot-scope="scope">
          <div v-for="val in scope.row.skuAttrRelationList" style="display: flex;justify-content: space-between;">
            <span> {{ val.attributeName }}：</span>
            <span> {{ val.value }}{{ val.unit }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="quantity" label="现有库存" align="center" width="80"/>
      <el-table-column prop="lockInventory" label="锁定库存" align="center" width="80"/>
      <el-table-column prop="safeInventory" label="安全库存" align="center" width="80"/>
      <el-table-column prop="costPrice" label="成本价(元)" align="center" width="90"/>
      <el-table-column prop="price" label="零售价(元)" align="center" width="100"/>
      <el-table-column prop="minPrice" label="最低零售价(元)" align="center" width="110"/>
      <el-table-column label="操作" align="center" width="130">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="updateRow(1,scope.row)"
          >详情
          </el-button>
          <el-button
            type="text"
            v-hasPermi="['ims:goodAttr:update']"
            @click="updateRow(2,scope.row)"
          >修改
          </el-button>
          <el-button
            type="text"
            v-hasPermi="['ims:goodAttr:delete']"
            @click="updateRow(3,scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination :total="total" :page.sync="form.pageNum" :limit.sync="form.pageSize" @pagination="getList"/>
    <updateDialog ref="updateDialog" @ok="handleQuery"></updateDialog>
  </div>
</template>

<script>
import {deleteGoodSku, listGoodSku} from "../../../api/goodMng";
import updateDialog from "./updateDialog.vue";
import {formatDay} from "../../../utils";

export default {
  name: 'Good',
  dicts: ['basic_good_source'],
  components: {updateDialog},
  data() {
    return {
      loading: false,
      list: [],
      tabIndex: '0',
      form: {
        productName: '',
        skuSource: '',
        skuName: '',
        pageNum: 1,
        pageSize: 10
      },
      total: 0
    }
  },
  created() {
    this.load()
  },
  methods: {
    load() {
      this.handleQuery()
    },
    changTab(e) {
      this.tabIndex = e.index
      this.handleQuery()
    },
    handleQuery() {
      this.form.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.resetForm('form')
      this.handleQuery()
    },
    getList() {
      this.loading = true
      this.form.productType = Number(this.tabIndex) + 1
      listGoodSku(this.form).then(res => {
        this.list = res.rows
        this.total = res.total
      }).finally(e => {
        this.loading = false
      })
    },
    handleExport() {
      this.download('/ims/productSku/export', {
        ...this.form
      }, `规格列表_${formatDay(new Date())}.xlsx`)
    },
    updateRow(flag, row) {
      let form = JSON.parse(JSON.stringify(row))
      form.tabIndex = this.tabIndex
      if (flag != 3) {
        this.$refs.updateDialog.show(flag, form)
      }
      if (flag == 3) {
        this.$modal.confirm('是否确定删除').then(() => {
          deleteGoodSku(row.id).then(res => {
            this.handleQuery()
            this.$message.success('删除成功')
          })
        })
      }
    }
  }
}
</script>
